/**
 * 
 */
package com.rangers.auth.base.details;

import java.security.Principal;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 授权用户信息
 * 
 * @version 1.0
 * @author fuhw
 * @date 2017年8月17日 下午5:26:49
 */
public class AuthUser implements UserDetails, Principal {

	private static final long serialVersionUID = -7057335799386059912L;

	private String token;

	private String username;// 账户名

	private String password;// 密码

	private Long scopeId;// 所属范围

	private boolean isEnabled;

	private Map<String, Collection<? extends GrantedAuthority>> authContexts;

	public AuthUser(String token, String username, String password, Long scopeId, boolean isEnabled,
			Map<String, Collection<? extends GrantedAuthority>> authContexts) {
		super();
		this.token = token;
		this.username = username;
		this.password = password;
		this.scopeId = scopeId;
		this.isEnabled = isEnabled;
		this.authContexts = authContexts == null ? Collections.emptyMap() : authContexts;
	}

	public AuthUser() {
		super();
		this.authContexts = Collections.emptyMap();
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Long getScopeId() {
		return scopeId;
	}

	public void setScopeId(Long scopeId) {
		this.scopeId = scopeId;
	}

	public Map<String, Collection<? extends GrantedAuthority>> getAuthContexts() {
		return authContexts;
	}

	public void setAuthContexts(Map<String, Collection<? extends GrantedAuthority>> authContexts) {
		this.authContexts = authContexts;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.security.Principal#getName()
	 */
	@Override
	public String getName() {
		return this.username;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.security.core.userdetails.UserDetails#getAuthorities(
	 * )
	 */
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		if (!authContexts.isEmpty()) {
			return authContexts.get(scopeId);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetails#
	 * isAccountNonExpired()
	 */
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetails#
	 * isAccountNonLocked()
	 */
	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetails#
	 * isCredentialsNonExpired()
	 */
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.security.core.userdetails.UserDetails#isEnabled()
	 */
	@Override
	public boolean isEnabled() {
		return isEnabled;
	}

	public void setEnabled(boolean isEnabled) {
		this.isEnabled = isEnabled;
	}

	
}
